package fr.isis.cloud;

import java.io.IOException;
import javax.servlet.http.*;
import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.SendResponse;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;

@SuppressWarnings("serial")
public class ReceiveChatMessage extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		XMPPService xmpp = XMPPServiceFactory.getXMPPService();
		Message message = xmpp.parseMessage(req);

		JID fromJid = message.getFromJid();
		String body = message.getBody();

		JID jid = new JID("julien.plagnes@gmail.com");

		Message msg = new MessageBuilder().withRecipientJids(jid)
				.withBody("Vous m'avez dit \"" + body + "\"").build();

		boolean messageSent = false;
		XMPPService xmpp1 = XMPPServiceFactory.getXMPPService();
		if (xmpp1.getPresence(jid).isAvailable()) {
			SendResponse status = xmpp1.sendMessage(msg);
			messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);
		}
	}
}